package com.woniu.p2p.controller;


import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.InvestComment;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserInvest;
import com.woniu.p2p.service.InvestDetailsService;
import com.woniu.p2p.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/details")
public class InvestDetailsController {

    @Autowired
    InvestDetailsService investDetailsService;
    //细节页面展示
    @RequestMapping("/investDetail/{id}")
    public InvestDetailsVo findAll(@PathVariable("id")int id){
        InvestDetailsVo allById = investDetailsService.findAllById(id);
        return allById;
    }
    //增加评论
    @RequestMapping("/addComment")
    public int insertComment(@RequestBody InvestComment ic, HttpServletRequest request){
        String token=request.getHeader("token");
        Integer i1 = Integer.parseInt(token);
        if(i1==null){
            return 0;
        }else {
            User allUserById = investDetailsService.findAllUserById(i1);
            String userNickname = allUserById.getUserNickname();
            Integer userId = allUserById.getUserId();
            String userHead = allUserById.getUserHead();
            Date date = new Date();
            String commentContext = ic.getCommentContext();
            Integer investId = ic.getInvestId();
            String commentImage="comment_pic02.png";
            InvestComment investComment = new InvestComment(userNickname,commentContext,date,investId,commentImage,userId);
            Integer i = investDetailsService.InsertComment(investComment);
            if(i!=null){
                return 1;
            }else {
                return 0;
            }
        }
    }
    //评论也查询分页展示
    @RequestMapping("/getComment/{id}")
    public PageInfo<InvestDetailsCommentsVo> getComment(@PathVariable("id")int id){
        int start=1;
        int size=3;
        PageInfo<InvestDetailsCommentsVo> pageInfo = investDetailsService.findAllCommentById(id, start, size);
        return pageInfo;
    }

    //投资的增加
    /**
     * 融资余额，投资进度，用户跟投资表，总记录表，减少用户的余额,修改投资表
     * @param ic
     * @param request
     * @return
     */
    @RequestMapping("/addInvest")
    public int insertUserInvest(@RequestBody InvestDetailsUserInvestVo ic, HttpServletRequest request){
        String token=request.getHeader("token");
        Integer i1 = Integer.parseInt(token);
        if(i1==null){
            return 0;
        }else {
            Integer id = ic.getId();
            long money = ic.getMoney();
            InvestDetailsVo allById = investDetailsService.findAllById(id);
            InvestDetailsVo allById1 = investDetailsService.findAllById(id);
            //融资余额的处理
            long invesBalance = allById.getInvesBalance();
            if(money>invesBalance){
                return 0;
            }else {
                if(money>allById1.getInvesBalance()){
                    return 0;
                }else {
                    if(money<allById1.getInvestStartpay()){
                        return 0;
                    }else {
                        long dealmoney = invesBalance - money;
                        //投资进度处理
                        long investAmount = allById.getInvestAmount();
                        double subribe = (dealmoney/(double)(investAmount))*100 ;
                        //投资时间记录
                        Date date = new Date();
                        investDetailsService.insertUserInvest(dealmoney,subribe,date,i1,id,money);
                        return 1;
                    }
                }
            }
        }
    }

    //还款明细的查询分页展示
    @RequestMapping("/getReimb/{id}")
    public PageInfo<InvestDetailsReimbVo> getReimb(@PathVariable("id")int id){
        int start=1;
        int num=3;
        PageInfo<InvestDetailsReimbVo> allInvestDetailsReimbVoBuInvestId = investDetailsService.findAllInvestDetailsReimbVoBuInvestId(id, start, num);
        return allInvestDetailsReimbVoBuInvestId;
    }


    //子公司的查询分页展示
    @RequestMapping("/getMinCompany/{id}")
    public PageInfo<InvestDetailsCompanyVo> getMinCompany(@PathVariable("id")int id){
        int start=1;
        int num=3;
        PageInfo<InvestDetailsCompanyVo> allInvestDetailsCompanyVoByInvestId = investDetailsService.findAllInvestDetailsCompanyVoByInvestId(id, start, num);
        return allInvestDetailsCompanyVoByInvestId;
    }


    //用户投标记录
    @RequestMapping("/getUserTou/{id}")
    public PageInfo<UserInvest> getUserTou(@PathVariable("id")int id){
        int start=1;
        int num=5;
        PageInfo<UserInvest> pageInfo = investDetailsService.findAllUserTou(start,num,id);
        return pageInfo;
    }



}
